package cn.bx.bid.service;


import cn.bx.bid.entity.*;
import cn.bx.bid.repository.ProfessorRepository;
import cn.bx.bid.repository.RandomNameRepository;
import cn.bx.bid.repository.WorkHistoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//M V C
@Service
public class ProfessorService  {
	public static final int PAGE_SIZE = Page.PAGE_SIZE;
@Autowired
	ProfessorRepository professorRepository;
@Autowired
	WorkHistoryRepository workHistoryRepository;
	@Autowired
	RandomNameRepository randomNameRepository;

//分页
public org.springframework.data.domain.Page<Professor> paging(int pageNo, int pageSize, String name, String  telephone, String  idcard){
	Sort sort =Sort.by(Sort.Order.desc("id")); //排序

	PageRequest p=PageRequest.of(pageNo,pageSize,sort);
	return  professorRepository.findAll(new Specification<Professor>(){
		@Override
		public Predicate toPredicate(Root<Professor> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
			List<Predicate> predicateList = new ArrayList<>();
			if (name != null) {
				predicateList.add((criteriaBuilder.like(root.get("professorName"), "%" + name + "%")));

			}
                if (telephone != null) {
                    predicateList.add((criteriaBuilder.like(root.get("mobliePhone"), "%" + telephone + "%")));

                }
			if (idcard != null) {
				predicateList.add((criteriaBuilder.like(root.get("identityNumber"), "%" + idcard + "%")));

			}

			criteriaQuery.where(predicateList.toArray(new Predicate[0]));
			return criteriaQuery.getRestriction();
		}

	},p);
}

//get
	public Professor get(long id){
	return  professorRepository.getOne(id);
	}

    //增加专家 addprofessor
	public int  addprofessor(Professor professor){
		Professor professor1=professorRepository.save(professor);
	    return 1;
	}

	//修改专家  updataprofessor
	public int  updataprofessor(Professor professor){
		Professor professor1=professorRepository.saveAndFlush(professor);
		return 1;
	}

	//删除专家 delprofessor
	public int  delprofessor(Long id){
		professorRepository.deleteById(id);
		return 1;
	}

	//批量删除  batchDel
	public int batchDel(long[] ids) {
		for (long id : ids) {

			this.professorRepository.deleteById(id);//对象
		}
		return ids.length;
	}

    //专家工作历史  getHistory
  public List<WorkHistory> getHistory(long pid){
    return 	workHistoryRepository.findAllByProfessorid(pid);
}


	// jionprosdel
	public  int  jionprosdel(long projid,long profid){
	randomNameRepository.deleteByProjectidAndProfessorid(projid,profid);
	return  1;
	}


	//getprojects
	public List<RandomName> getprojects(long professorId){
		return  randomNameRepository.findAllByProfessorid(professorId);
	}

	//jionprojectAdd 增加参与项目抽取记录
	public  int  jionprojectAdd(RandomName randomName){
	randomNameRepository.save(randomName);
	return 1;
	}

	//增加工作历史  addHistory
	public int addHistory(WorkHistory workHistory){
	workHistoryRepository.save(workHistory);
	return 1;
	}

	//获得该工作历史 getOneHistory
	public WorkHistory getOneHistory(long id){
		return 	workHistoryRepository.getOne(id);
	}

	//修改工作历史  historyupdate
	public int historyupdate(WorkHistory workHistory){
		workHistoryRepository.saveAndFlush(workHistory);
		return 1;
	}
	//删除工作历史 delHis
	public int delHis(long pid,long hisid){
		workHistoryRepository.deleteByProfessoridAndId(pid,hisid);
		return 1;
	}



}
